package com.dcivision.user.core;

import java.sql.Connection;

import com.dcivision.dms.core.RootOperationManager;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.Utility;
import com.dcivision.staff.bean.StaffRecord;
import com.dcivision.staff.dao.StaffRecordDAObject;
import com.dcivision.user.bean.MtmUserRecordUserGroup;
import com.dcivision.user.bean.MtmUserRecordUserRole;
import com.dcivision.user.bean.UserRecord;
import com.dcivision.user.dao.MtmUserRecordUserGroupDAObject;
import com.dcivision.user.dao.MtmUserRecordUserRoleDAObject;
import com.dcivision.user.dao.UserRecordDAObject;

public class UserManager {
  
  private SessionContainer sessionContainer = null;
  private Connection conn = null;
  
  public UserManager(SessionContainer sessionContainer,Connection conn){
    this.sessionContainer = sessionContainer;
    this.conn = conn;
  }
  
  public UserRecord createUser(UserRecord userRecord,String[] roleIDs,String[] groupIDs) throws ApplicationException{
    UserRecord user = createUser(userRecord);
    //  Insert the user groups belonging to the added user record.
    if (!Utility.isEmpty(groupIDs)){
      MtmUserRecordUserGroupDAObject mtmUserRecordUserGroupDAO = new MtmUserRecordUserGroupDAObject(sessionContainer,conn);
      for (int i=0;i<groupIDs.length;i++){
        MtmUserRecordUserGroup mtmGroupBean = new MtmUserRecordUserGroup();
        mtmGroupBean.setUserRecordID(user.getID());
        if (!Utility.isEmpty(groupIDs[i]) && !"".equals(groupIDs[i])){
          mtmGroupBean.setUserGroupID(new Integer(groupIDs[i]));
          mtmUserRecordUserGroupDAO.insertObject(mtmGroupBean);  
        }
      }
    }
    
    //  Insert the user groups belonging to the added user record.
    if (!Utility.isEmpty(roleIDs)){
      MtmUserRecordUserRoleDAObject mtmUserRecordUserRoleDAO = new MtmUserRecordUserRoleDAObject(sessionContainer,conn);
      for (int i=0;i<roleIDs.length;i++){
        MtmUserRecordUserRole mtmRoleBean = new MtmUserRecordUserRole();
        mtmRoleBean.setUserRecordID(user.getID());
        if (!Utility.isEmpty(roleIDs[i]) && !"".equals(roleIDs[i])){
          mtmRoleBean.setUserRoleID(new Integer(roleIDs[i]));
          mtmUserRecordUserRoleDAO.insertObject(mtmRoleBean);
        }        
      }
    }
    return user;
  }
  
  public UserRecord createUser(UserRecord userRecord) throws ApplicationException{
    if (UserRecord.USER_TYPE_STAFF.equals(userRecord.getUserType())){
      StaffRecord staffRecord = new StaffRecord();
      staffRecord.setFirstName(userRecord.getFirstName());
      staffRecord.setLastName(userRecord.getLastName());
      staffRecord.setFullName(userRecord.getFullName());
      staffRecord.setEmail(userRecord.getEmail());
      staffRecord.setGender("M");
      staffRecord.setSalutationID(new Integer(1));
      staffRecord.setStaffCode("N/A");
      staffRecord.setStatus("A");
      staffRecord = createStaffRecord(staffRecord);
      if (!Utility.isEmpty(staffRecord.getID())){
        userRecord.setUserLinkID(staffRecord.getID());
      }
    }
    //insert Record
    UserRecord user = new UserRecord();
    userRecord.setUserType("S");
    UserRecordDAObject userRecordDAO = new UserRecordDAObject(sessionContainer,conn);
    user = (UserRecord)userRecordDAO.insertObject(userRecord);
    
    //inital document persional folder
    initDOCRelatedUserRecord(user); 
    initFlowRelatedUserRecord(user); 
    return user;
  }
  
  public StaffRecord createStaffRecord(StaffRecord staff) throws ApplicationException{
    StaffRecordDAObject staffDAO = new StaffRecordDAObject(sessionContainer, conn);
    return (StaffRecord)staffDAO.insertObject(staff);
  }
  
  protected void initDOCRelatedUserRecord(UserRecord userRecord) throws ApplicationException{
    RootOperationManager rootOpMgr = new RootOperationManager(sessionContainer,conn);
    rootOpMgr.createDefaultPersonalRootPointForUser(userRecord.getID());
//    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer,conn); 
//    docRetrievalManager.createMyFavoriteFolder()
    
  }
  
  protected void initFlowRelatedUserRecord(UserRecord userRecord){
    
  }
}
